package ynu.edu.service;

import org.aspectj.weaver.ast.Or;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import ynu.edu.common.service.LogicService;
import ynu.edu.dao.*;
import ynu.edu.entity.Item;
import ynu.edu.entity.Order;
import ynu.edu.entity.Shop;
import ynu.edu.entity.ShopItem;

import javax.annotation.Resource;
import java.util.List;

@Service
public class ShopService extends LogicService<IShopDao, Shop,Long> {
    public ShopService(@Autowired IShopDao dao) {
        super(dao);
    }
    @Resource
    private IShopItemDao shopItemDao;
    @Resource
    private IItemDao itemDao;
    @Resource
    private IOrderDao orderDao;

    public Shop getShopById(Long shopId){
        return getDAO().getReferenceById(shopId);
    }

    //
    public List<ShopItem> listMyShopItemsIsOnShelves(Long ShopId){
        Shop shop = getDAO().getReferenceById(ShopId);
        return shop.listMyShopItemsIsOnShelves();
    }

    public List<ShopItem> listMyShopItems(Long ShopId){
        Shop shop = getDAO().getReferenceById(ShopId);
        return shop.listMyShopItems();
    }

    public ShopItem createShopItem(Long shopId,Long itemId,Double price){
        Shop shop = getDAO().getReferenceById(shopId);
        Item item = itemDao.getReferenceById(itemId);
        ShopItem shopShopItem = shop.createShopItem(item, price);
        getDAO().save(shop);
        return shopShopItem;
    }

    public ShopItem updateShopItem(Long shopId,Long shopItemId){
        Shop shop = getDAO().getReferenceById(shopId);
        ShopItem shopShopItem = shopItemDao.getReferenceById(shopItemId);
        shop.updateShopItem(shopShopItem);
        getDAO().save(shop);
        return shopShopItem;
    }

    public List<Shop> getAllShops(){
        return getDAO().findAll();
    }

    public List<ShopItem> getShopItems(Long shopId){
        Shop shop = getDAO().getReferenceById(shopId);
        return shop.listMyShopItemsIsOnShelves();
    }

    public List<Order> getMyShopOrder(Long shopId,Integer orderState){
        Shop referenceById = getDAO().getReferenceById(shopId);
        return referenceById.listMyOrders(orderState);
    }

    public Order updateOrderState(Long shopId,Long orderId,Integer orderState){
        Shop shop = getDAO().getReferenceById(shopId);
        Order order = orderDao.getReferenceById(orderId);
        shop.updateOrderState(order, orderState);
        getDAO().save(shop);
        return orderDao.getReferenceById(orderId);
    }

}
